/* BSD - License

Copyright (c) 2010, Kevin Hall
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

* The names of contributors may not be used to endorse or promote products
  derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _DEELX_STL_H__
#define _DEELX_STL_H__

#include <string>
#include <exception>
#include <stdexcept>
#include "deelx.h"

#define REGEX_WORD_CHAR			"\\w"
#define REGEX_NON_WORD_CHAR		"\\W"
#define REGEX_SPACE				"\\s"
#define REGEX_NON_SPACE			"\\S"
#define REGEX_DIGIT				"\\d"
#define REGEX_NON_DIGIT			"\\D"
#define REGEX_ALPHA				"[:alpha:]"
#define REGEX_NON_ALPHA			"[:^alpha:]"

#define REGEX_ALPHANUM			"[:alnum:]"
#define REGEX_ASCII				"[:ascii:]"
#define REGEX_CONTROL			"[:cntrl:]"
#define REGEX_PRINTABLE			"[:print:]"
#define REGEX_GRAPH				"[:graph:]"
#define REGEX_LOWER				"[:lower:]"
#define REGEX_UPPER				"[:upper:]"
#define REGEX_PUNCT				"[:punct:]"
#define REGEX_HEXDIGIT			"[:xdigit:]"
#define REGEX_BLANK				"[:blank:]"

#define REGEX_NON_ALPHANUM		"[:^alnum:]"
#define REGEX_NON_ASCII			"[:^ascii:]"
#define REGEX_NON_CONTROL		"[:^cntrl:]"
#define REGEX_NON_PRINTABLE		"[:^print:]"
#define REGEX_NON_GRAPH			"[:^graph:]"
#define REGEX_NON_LOWER			"[:^lower:]"
#define REGEX_NON_UPPER			"[:^upper:]"
#define REGEX_NON_PUNCT			"[:^punct:]"
#define REGEX_NON_HEXDIGIT		"[:^xdigit:]"
#define REGEX_NON_BLANK			"[:^blank:]"


namespace deelx_stl
{
	class no_match_error : public std::runtime_error
	{
	public:
		no_match_error(const std::string& message = "No match found")
			: std::runtime_error(message)
		{}
	};

	class range_error : public std::range_error
	{
	public:
		range_error(const std::string& message = "Out of range")
			: std::range_error(message)
		{}
	};

	enum flags
	{
		no_flag			= NO_FLAG,
		single_line		= SINGLELINE,
		multi_line		= MULTILINE,
		global			= GLOBAL,
		ignore_cast		= IGNORECASE,
		right_to_left	= RIGHTTOLEFT,
		extended		= EXTENDED
	};

	template<class T>
	class basic_regex
	{
		typename CRegexpT<T>	re;

	public:
		typedef T						char_t;
		typedef std::basic_string<T>	string_t;

		class match_result
		{
			friend class basic_regex<T>;

			MatchResult	mr;
			string_t	source_text;

			match_result(const MatchResult& mr_, const char_t* text_)
				: mr(mr_)
				, source_text(text_)
			{}
		public:

			bool matched() const
			{
				return (mr.IsMatched() != 0);
			}

			operator bool() const
			{
				return matched();
			}

			bool operator!() const
			{
				return !matched();
			}

			const string_t& source() const
			{
				return source_text;
			}

			string_t text() const
			{
				if (!matched())
				{
					throw no_match_error();
				}
				int start = mr.GetStart();
				return source_text.substr(start, mr.GetEnd()-start);
			}

			int start() const
			{
				return mr.GetStart();
			}

			int end() const
			{
				return mr.GetEnd();
			}

			int groups() const
			{
				return 1+mr.MaxGroupNumber();
			}

			int group_start(int group_index)
			{
				if ((group_index < 0) || (group_index >= groups()))
				{
					throw range_error();
				}
				return mr.GetGroupStart(group_index);
			}

			int group_end(int group_index)
			{
				if ((group_index < 0) || (group_index >= groups()))
				{
					throw range_error();
				}
				return mr.GetGroupEnd(group_index);
			}

			string_t group_text(int group_index)
			{
				if ((group_index < 0) || (group_index >= groups()))
				{
					throw range_error();
				}
				int start = mr.GetGroupStart(group_index);
				return source_text.substr(start, mr.GetGroupEnd(group_index)-start);
			}
		};

		basic_regex(const char_t* pattern, int flags = no_flag)
			: re(pattern, flags)
		{}

		basic_regex(const char_t* pattern, int length, int flags)
			: re(pattern, length, flags)
		{}

		basic_regex(const string_t& pattern, int flags = no_flag)
			: re(pattern.c_str(), flags)
		{}

		basic_regex(const string_t& pattern, int length, int flags)
			: re(pattern.c_str(), length, flags)
		{}

		match_result match(const char_t* str) const
		{
			return match_result(re.Match(str), str);
		}

		match_result match(const char_t* str, int start) const
		{
			return match_result(re.Match(str, start), str);
		}

		match_result match(const string_t& str) const
		{
			return match(str.c_str());
		}

		match_result match(const string_t& str, int start) const
		{
			return match(str.c_str(), start);
		}

		match_result match_exact(const char_t* str) const
		{
			return match_result(re.MatchExact(str), str);
		}

		match_result match_exact(const string_t& str) const
		{
			return match_exact(str.c_str());
		}
	};

	typedef basic_regex<char>		regex;
	typedef basic_regex<wchar_t>	wregex;
}

#endif //#ifndef _DEELX_STL_H__
